define(
		[ "PDAppDir/properties", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar",
				"PDUtilDir/inputSelect",
				"PDUtilDir/fileupload/singleFileUpload", "PDUtilDir/wangEditor.min", "AppPathDir/utils", "PDUtilDir/validator", "Date", "DateCN",
				"css!DateCss", "PicView", "css!picViewCss",
				"css!AppPathDir/product/productInfos/views/css/css" ],
		function(Properties, Util, Tool, Slidebar, inputSelect, MFU, E, Common, validator) {
			function updateSupportInit($scope, $compile) {
				return new updateSupport($scope, $compile);
			}
			function updateSupport($scope, $compile) {
				this.validator = validator;
				
				this.$scope = $scope;
				this.$compile = $compile;
			}
			updateSupport.fn = updateSupport.prototype;
			updateSupport.fn.digest = function() {
				this.$scope.$digest();
			}
			updateSupport.fn.init = function(data) {
				var _this = this, tmp_obj = {};
				this.entity = {
					trip : new Array(),
					timetable : new Array(),
					explain : new Map(),
					image: new Map(),
					lineId : ""
				};
				this.cache = {
					isOpen: true,
					isRender: false,
					path: Properties.URL_FILE_SERVER_URL,
                    feature_editor: undefined,
                    guarantee_editor: undefined,
                    explainNotes_editor: undefined,
                    explainCosts_editor: undefined,
                    customTripName_editor: undefined
				};
				this.bind = {};
				// 预定须知，费用说明
				$.each(data.explains, function(index, row) {
					tmp_obj[row.uuid] = row.description;
				});
				//出发地，目的地，图片，班期
				_this.entity = $.extend(_this.entity, data.product, {
					startPlaceProvince: data.startPlace.province,
					startPlaceCity: data.startPlace.city,
					endPlaceProvince: data.endPlace.province,
					endPlaceCity: data.endPlace.city,
					image: {
						resouceId: data.resources[0].resouceId,
						path: data.resources[0].path
					},
					explain: tmp_obj
				});
				//行程
				// $.each(data.trip, function(index, row) {
				// 	_this.entity.trip.push({
				// 		id: row.id,
				// 		sort: row.sort,
				// 		vehicle: {
				// 			bus: (row.bus == "true"),
				// 			train: (row.train == 'true'),
				// 			ship: (row.ship == 'true'),
				// 			subway: (row.subway == 'true'),
				// 			hsubway: (row.hsubway == 'true'),
				// 			plane: (row.plane == 'true')
				// 		},
				// 		meal: {
				// 			lunch: (row.lunch == 'true'),
				// 			nothing: (row.nothing == 'true'),
				// 			dinner: (row.dinner == 'true'),
				// 			breakfast: (row.breakfast == 'true'),
				// 			aircraft: (row.aircraft == 'true')
				// 		},
				// 		resource: $.extend({}, row.resource[0]),
				// 		isAbroad: row.isAbroad == 'true',
                 //        describe: row.description,
				// 		stayDescribe: row.stayDescribe
				// 	});
				// });
				_this.baseInit();
				// _this.tripInit(false);
				_this.digest();
			}
			updateSupport.fn.run = function(id) {
				var _this = this;
				_this.id = id;
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer()
							+ "/static/app/product/productInfos/views/updateProduct.html",
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function($Panel) {
						_this.initialization();
					}
				});
			};
			updateSupport.fn.initialization = function() {
				var _this = this;
				// 获取产品详情
				$.ajax({
					url : getServer() + "/flea/hclv/product/queryDetailById.htmls",
					data : {
						'id' : _this.id
					},
					success : function(data) {
						_this.init(data.data);
					}
				});
			};
			updateSupport.fn.baseInit = function() {
				var _this = this;
				// 类别初始化下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "typeName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.types,
					simpleData : true,
					// 启用文本搜索
					initData:[ _this.entity.type ],
					searchAble : false,
					callback : function(id) {
						_this.entity.type = id[0];
						_this.$scope.updateProductForm.type.$setViewValue(_this.entity.type);
						_this.queryAllLines(_this.entity.type);
					}
				});

				// 初始化出发地省下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "add_startPlaceProvince",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.provinces,
					simpleData : true,
					// 启用文本搜索
					initData:[_this.entity.startPlaceProvince],
					searchAble : false,
					callback : function(id) {
						_this.entity.startPlaceProvince = id[0];
						_this.$scope.updateProductForm.startPlaceProvince.$setViewValue(_this.entity.startPlaceProvince);
						_this.getCity('start', _this.entity.startPlaceProvince);
					}
				});

				// 初始化目的地省下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "add_endPlaceProvince",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.provinces,
					simpleData : true,
					// 启用文本搜索
					initData:[_this.entity.endPlaceProvince],
					searchAble : false,
					callback : function(id) {
						_this.entity.endPlaceProvince = id[0];
						_this.$scope.updateProductForm.endPlaceProvince.$setViewValue(_this.entity.endPlaceProvince);
						_this.getCity('end', _this.entity.endPlaceProvince);
					}
				});
				// 初始化产品类别下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "add_productType",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : [ {
						"dictCode" : "self",
						"dictName" : "自营"
					}, {
						"dictCode" : "unself",
						"dictName" : "非自营"
					} ],
					simpleData : true,
					// 启用文本搜索
					initData:[_this.entity.productCategory],
					searchAble : false,
					callback : function(id) {
						_this.entity.productCategory = id[0];
						_this.$scope.updateProductForm.productCategory.$setViewValue(_this.entity.productCategory);
					}
				});
				//海报
				MFU.init({
					placeAt : "id_img",
					allowDelete : true,
					allowMC : true,
					onUploadSuccess : function(file) {
						_this.entity.image = $.extend(_this.entity.image, {
							'key' : file.name,
							'resouceId' : file.swordFileId,
							'path' : file.swordFilePath
						});
					},
					onDeleteSavedFile:function(file){
						if(file == null)
						delete _this.entity.image;
	                    _this.digest();
	                },
					onFileQueued : function(file) {
                        if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                            Util.alert(ImageSuffixNotSupport);
                            this.remove();
                            return;
                        }
						this.webUploader.makeThumb(file, function(error, src) {
							if (error) {
								Util.alert('不能预览');
								return;
							}
							_this.entity.image = { src: src }
							_this.digest();
						}, 500, 300);
						this.webUploader.upload();
					}
				});
                // 上传行程文档
                MFU.init({
                    placeAt: "trip_doc_file",
                    allowDelete: true,
                    allowMC: true,
                    onUploadSuccess: function (file) {
                        _this.entity.fileTripId = file.swordFileId;
                        _this.$scope.updateTripFileUploadForm.fileTripId.$setViewValue(_this.entity.fileTripId);
                        _this.entity.tripFilePath = undefined;
                        _this.digest();
                    },
                    onDeleteSavedFile:function(file){
                        if(file == null)
                            return;
                        _this.entity.fileTripId = undefined;
                        _this.$scope.updateTripFileUploadForm.fileTripId.$setViewValue(_this.entity.fileTripId);
                        _this.digest();
                    },
                    onFileQueued: function (file) {
                        if(DocSuffix.indexOf(file.ext.toLowerCase()) < 0){
                            Util.alert(DocSuffixNotSupport);
                            this.remove();
                            return;
                        }
                        this.webUploader.upload();
                    }
                });
				//特色福文本框
                _this.cache.feature_editor = new E('#feature');
                _this.cache.feature_editor.customConfig.onchange = function (html) {
					_this.entity.feature = html;
					_this.$scope.updateProductForm.feature.$setViewValue(html);
			    };
                _this.cache.feature_editor.customConfig.menus = Common.wangEditorMenus;
                _this.cache.feature_editor.create();
                _this.cache.feature_editor.txt.html(_this.entity.feature);
		        
		        //服务保障福文本框
                _this.cache.guarantee_editor = new E('#guarantee');
                _this.cache.guarantee_editor.customConfig.onchange = function (html) {
					_this.entity.guarantee = html;
					_this.$scope.updateProductForm.guarantee.$setViewValue(html);
			    };
                _this.cache.guarantee_editor.customConfig.menus = Common.wangEditorMenus;
                _this.cache.guarantee_editor.create();
                _this.cache.guarantee_editor.txt.html(_this.entity.guarantee);
		        
		        //预定须知福文本框
                _this.cache.explainNotes_editor = new E('#explainNotes');
                _this.cache.explainNotes_editor.customConfig.onchange = function (html) {
					_this.entity.explain.notes = html;
					_this.$scope.updateEXPLAINForm.explainNotes.$setViewValue(html);
                };
                _this.cache.explainNotes_editor.customConfig.menus = Common.wangEditorMenus;
                _this.cache.explainNotes_editor.create();
                _this.cache.explainNotes_editor.txt.html(_this.entity.explain.notes);
		        
		        //费用说明福文本框
                _this.cache.explainCosts_editor = new E('#explainCosts');
                _this.cache.explainCosts_editor.customConfig.onchange = function (html) {
					_this.entity.explain.costs = html;
					_this.$scope.updateEXPLAINForm.explainCosts.$setViewValue(html);
                };
                _this.cache.explainCosts_editor.customConfig.menus = Common.wangEditorMenus;
                _this.cache.explainCosts_editor.create();
                _this.cache.explainCosts_editor.txt.html(_this.entity.explain.costs);

                //自定义行程福文本框
                // _this.cache.customTripName_editor = new E('#customTripName');
                // _this.cache.customTripName_editor.customConfig.onchange = function (html) {
                //     _this.entity.customTrip = html;
                //     _this.$scope.updateTripCustomForm.customTrip.$setViewValue(html);
                // };
                // _this.cache.customTripName_editor.customConfig.menus = ['image'].concat(Common.wangEditorMenus);
                // _this.cache.customTripName_editor.customConfig.uploadImgShowBase64 = true
                // _this.cache.customTripName_editor.create();
                // _this.cache.customTripName_editor.txt.html(_this.entity.customTrip);

		        //线路
				_this.queryAllLines(_this.entity.type);
				_this.getCity('start', _this.entity.startPlaceProvince);
				_this.getCity('end', _this.entity.endPlaceProvince);
			};
			updateSupport.fn.tripInit = function() {
				var _this = this;

				_this.$scope.$watch('updateSupport.entity.travelDay', function( newValue, oldValue) {
					_this.cache.isRender = false;
					if(_this.cache.isOpen){
						_this.cache.isOpen = false;
						return;
					}
					_this.entity.trip = [];
					for (var i = 0; i < newValue; i++) {
						_this.entity.trip.push({
							sort : (i + 1),
							vehicle : {
								bus : false,
								train : false,
								ship : false,
								subway : false,
								hsubway : false,
								plane : false
							},
							meal : {
								breakfast : false,
								lunch : false,
								dinner : false,
								nothing : false,
								aircraft : false
							},
							describe : undefined,
							stayDescribe : undefined,
							isAbroad : 0
						});
					}
				});
			};
			//获取城市数据
			updateSupport.fn.getCity = function(type, pcode) {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/getCity.htmls",
					data : {
						'pcode' : pcode
					},
					success : function(data) {
						var citys = data.data;
						if (type == "start") {
							_this.cache.startPlaceCity = data.data;
							_this.startPlaceCity();
						} else {
							_this.cache.endPlaceCity = data.data;
							_this.endPalceCity();
						}
					}
				});
			};
			//出发城市初始化下拉框
			updateSupport.fn.startPlaceCity = function() {
				var _this = this;
				// 初始化下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "add_startPlaceCity",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.cache.startPlaceCity,
					simpleData : true,
					// 启用文本搜索
					initData:[_this.entity.startPlaceCity],
					searchAble : false,
					callback : function(id) {
						_this.entity.startPlaceCity = id[0];
						_this.$scope.updateProductForm.startPlaceCity.$setViewValue(_this.entity.startPlaceCity);
					}
				});
			};
			//目的城市初始化下拉框
			updateSupport.fn.endPalceCity = function() {
				var _this = this;
				// 初始化下拉框
				inputSelect.dropCache();
				inputSelect({
					id : "add_endPalceCity",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.cache.endPlaceCity,
					simpleData : true,
					// 启用文本搜索
					initData:[_this.entity.endPlaceCity],
					searchAble : false,
					callback : function(id) {
						_this.entity.endPlaceCity = id[0];
						_this.$scope.updateProductForm.endPlaceCity.$setViewValue(_this.entity.endPlaceCity);
					}
				});
			};
			updateSupport.fn.removeImg = function(resouceId) {
				delete this.entity.image;
			};
			updateSupport.fn.tripRemoveImg = function(resouceId) {
				var _this = this;
                $.each(_this.entity.trip, function(index, row){
                	if(row.resource.resouceId == resouceId){
                		delete row.resource;
                		return;
                	}
                });
			};
			// 根据天数生成行程
			updateSupport.fn.init_fileupload = function() {
				var _this = this, data = _this.entity.trip;
				if(_this.cache.isRender)
					return;
				$.each(data, function(index, row) {
					MFU.init({
						placeAt : "id_" + (index + 1),
						allowDelete : true,
						allowMC : true,
						onUploadSuccess : function(file) {
							var id = this.settings.placeAt.substring(3) - 1, obj = _this.entity.trip[id];
							obj.resource = $.extend(obj.resource, {
								'resouceId' : file.swordFileId,
								'path' : file.swordFilePath
							});
						},
						onDeleteSavedFile:function(file){
							if(file == null)
								return;
							_this.tripRemoveImg(file.swordFileId)
		                    _this.digest();
		                },
						onFileQueued : function(file) {
                            if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                                Util.alert(ImageSuffixNotSupport);
                                this.remove();
                                return;
                            }

							var id = this.settings.placeAt.substring(3) - 1, webUploader = this.webUploader;
							webUploader.makeThumb(file, function(error, src) {
								if (error) { Util.alert('不能预览'); return; }
								_this.entity.trip[id].resource = {
									src : src
								};
								_this.digest();
							}, 500, 300);
							webUploader.upload();
						}
					});
				});
				_this.cache.isRender = true;
			};
			updateSupport.fn.save = function() {
				var _this = this;
				for(var item in _this.entity.trip){
					if(_this.entity.trip[item].resource.src)
						_this.entity.trip[item].resource.src = undefined;
					_this.entity.trip[item].image = _this.entity.trip[item].resource;
				}
				_this.entity.image.src = undefined;
				$.ajax({
					url : getServer() + "/flea/hclv/product/update.htmls",
					data : {
						'model' : JSON.stringify(_this.entity)
					},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						_this.$scope.grid.refresh();
						Util.alert(data.message);
						_this.slidebars.close();
					}
				});
			};
			updateSupport.fn.queryAllLines = function(type) {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/line/queryAll.htmls",
					data : {
						type : type
					},
					success : function(data) {
						inputSelect.dropCache();
						inputSelect({
							id : "lineName",
							key : {
								id : "id",
								name : "name",
								data : "data",
							},
							data : data,
							simpleData : true,
							// 启用文本搜索
							initData : [ _this.entity.lineId ],
							searchAble : false,
							callback : function(id, data) {
								_this.entity.lineId = id[0];
								_this.$scope.updateProductForm.lineId.$setViewValue(id[0]);
							}
						});
					}
				});
			};
			return updateSupportInit;
		});
